/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.BorderLayout;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import modelo.Articulo;
import modelo.Stockitem;
import modelo.tabla.StockTableModel;
import persistencia.BBDDgroup2;

/**
 *
 * @author MarioPT
 */
public class JPStock extends javax.swing.JPanel {

    BBDDgroup2 bbdd;
    StockTableModel modelotabla;
    JTable tablastock;
    ArrayList<Stockitem> datostabla;
    Articulo filtro; //recoge en un objeto de tipo articulo los filtros indicados
    JScrollPane jsp;
    JPanel paneltable;

    /**
     * Creates new form JpStock
     */
    public JPStock() {
        initComponents();
        this.setSize(800, 600);
        actualizardatos();
        jsp = new JScrollPane();
        modelotabla = new StockTableModel(datostabla);
        tablastock = new JTable(modelotabla);
        tablesice();
        tablastock.setFillsViewportHeight(true);
        jsp.setViewportView(tablastock);
        jsp.repaint();
        this.jPanel1.add(jsp, BorderLayout.CENTER);
        for (int i = 0; i < datostabla.size(); i++) {
            System.out.println(datostabla.get(i));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        TFidart = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        TFidalmacen = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        TFdescripcion = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        TFcantidad = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        setLayout(new java.awt.BorderLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Consultar stock");
        jLabel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 20, 20, 1));
        add(jLabel1, java.awt.BorderLayout.PAGE_START);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.GridLayout(3, 1));

        jLabel2.setText("ID de Artículo:");
        jPanel4.add(jLabel2);

        TFidart.setMinimumSize(new java.awt.Dimension(60, 30));
        TFidart.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel4.add(TFidart);

        jLabel3.setText("ID Almacen:");
        jPanel4.add(jLabel3);

        TFidalmacen.setMinimumSize(new java.awt.Dimension(60, 30));
        TFidalmacen.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel4.add(TFidalmacen);

        jLabel4.setText("Descripcion:");
        jPanel4.add(jLabel4);

        TFdescripcion.setMinimumSize(new java.awt.Dimension(60, 30));
        TFdescripcion.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel4.add(TFdescripcion);

        jLabel5.setText("Cantidad:");
        jPanel4.add(jLabel5);

        TFcantidad.setMinimumSize(new java.awt.Dimension(60, 30));
        TFcantidad.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel4.add(TFcantidad);

        jPanel2.add(jPanel4);

        jButton1.setText("Consultar");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton1MouseReleased(evt);
            }
        });
        jPanel3.add(jButton1);

        jButton2.setText("Limpiar");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton2MouseReleased(evt);
            }
        });
        jPanel3.add(jButton2);

        jPanel2.add(jPanel3);

        jPanel1.add(jPanel2, java.awt.BorderLayout.NORTH);

        add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseReleased
        updatetabledata();
    }//GEN-LAST:event_jButton1MouseReleased
    private void updatetabledata(){
            actualizardatos();
        modelotabla = new StockTableModel(datostabla);
        tablastock = new JTable(modelotabla);
        tablesice();
        tablastock.setFillsViewportHeight(true);
        jsp.setViewportView(tablastock);
        this.jPanel1.add(jsp, BorderLayout.CENTER);
        for (int i = 0; i < datostabla.size(); i++) {
            System.out.println(datostabla.get(i));
        jPanel1.repaint();
        }
    }

    
    private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseReleased
        this.TFcantidad.setText("");
        this.TFdescripcion.setText("");
        this.TFidalmacen.setText("");
        this.TFidart.setText("");
        this.updatetabledata();
    }//GEN-LAST:event_jButton2MouseReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField TFcantidad;
    private javax.swing.JTextField TFdescripcion;
    private javax.swing.JTextField TFidalmacen;
    private javax.swing.JTextField TFidart;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    // End of variables declaration//GEN-END:variables
    private void tablesice() {
        //Modificamos los tamaños de las columnas.
        tablastock.getColumnModel().getColumn(0).setMaxWidth(70);
        tablastock.getColumnModel().getColumn(1).setMaxWidth(70);
        tablastock.getColumnModel().getColumn(2).setMinWidth(100);
        tablastock.getColumnModel().getColumn(3).setMaxWidth(70);
        tablastock.getColumnModel().getColumn(4).setMaxWidth(70);
        tablastock.getColumnModel().getColumn(5).setMaxWidth(70);
    }

    private void actualizardatos() {
        try {
            /*
             Se encarga de pasar al modelo de la tabla un ArrayList de stocitem
             */

            bbdd = new BBDDgroup2();
        } catch (SQLException ex) {
            Logger.getLogger(JPStock.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(
                    this,
                    "Error de SQL al conextase a la BBDD",
                    "Consulta",
                    JOptionPane.INFORMATION_MESSAGE);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(JPStock.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(
                    this,
                    "Error de SQL al conectase a la BBDD",
                    "Consulta",
                    JOptionPane.INFORMATION_MESSAGE);
        }
        //Primero obtenemos los articulos actuales de la bbdd que cumplan los filtros
        ArrayList<Articulo> articulos = new ArrayList();
        //creamos el filtro en base a los textfielddel jpanel
        int idartaux;//recoge del texfield el id
        if (this.TFidart.getText().isEmpty()) {
            idartaux = -1;
        } else {
            idartaux = Integer.parseInt(this.TFidart.getText());
        }
        int idalmacenaux;
        if (this.TFidalmacen.getText().isEmpty()) {
            idalmacenaux = -1;
        } else {
            idalmacenaux = Integer.parseInt(this.TFidalmacen.getText());
        }
        int cantidadaux;
        if (this.TFcantidad.getText().isEmpty()) {
            cantidadaux = -1;
        } else {
            cantidadaux = Integer.parseInt(this.TFcantidad.getText());
        }

        filtro = new Articulo(
                idartaux,
                idalmacenaux,
                this.TFdescripcion.getText(),
                cantidadaux,
                0
        );

        
        try {
            articulos = bbdd.getarticulosfilter(filtro);
        } catch (SQLException ex) {
            Logger.getLogger(JPStock.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(
                    this,
                    "ERROR. Al realizar la consulta sobre los articulos BaseDatos.getArticulosFilter",
                    "Consulta",
                    JOptionPane.INFORMATION_MESSAGE);
        }
        
        //Ahora creamos el ArrayList de Stock en base a los pedidos
        Articulo articulo;
        datostabla = new ArrayList();
        if (articulos.size()!=0) {
            for (int i = 0; i < articulos.size(); i++) {

            articulo = articulos.get(i);
            System.out.println("" + articulo.toString());
                try {
                    datostabla.add(new Stockitem(articulo, bbdd.getPedidosfromArticulo(articulo)));
                } catch (SQLException ex) {
                    Logger.getLogger(JPStock.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(
                        this,
                        "ERROR. Al realizar la consulta sobre los articulos BaseDatos.getPedidosFromArticulo",
                        "Consulta",
                        JOptionPane.ERROR_MESSAGE);
                    
                }
            }
        }else{
            JOptionPane.showMessageDialog(
                    this,
                    "No ha devuelto resultados",
                    "Consulta",
                    JOptionPane.INFORMATION_MESSAGE);
        }
        
    }

    //fin clse
}
